package com.aplink.generic.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.aplink.generic.pojo.xml.RecordInfo;
import com.aplink.generic.pojo.xml.TableInfo;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.convertcount.GenericConverter;
import com.aplink.generic.xmlparser.RecordInfoParser;
import com.aplink.generic.xmlparser.TableInfoParser;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

public class DatabaseManager extends SQLiteOpenHelper {
    private SQLiteDatabase myDataBase;
    private ArrayList<RecordInfo> recordInfoList;
    private ArrayList<TableInfo> tableInfoList;
    private final LogUtil mLogUtil = new LogUtil( this.getClass() );
    private Context mContext;
    private int mTableStructureFileId;
    private int mTableDataFileId;

    public Context getContext() {
        return this.mContext;
    }

    public int getTableStructureFileId() {
        return this.mTableStructureFileId;
    }

    public int getTableDataFileId() {
        return this.mTableDataFileId;
    }

    public DatabaseManager( Context context, String databaseName,
                            int databaseVersion, int tableStructureFileId, int tableDataFileId ) {
        super( context, databaseName, null, databaseVersion );
        mContext = context;
        mTableDataFileId = tableDataFileId;
        mTableStructureFileId = tableStructureFileId;
    }

    @Override
    public synchronized void close() {
        if ( this.myDataBase != null ) {
            this.myDataBase.close();
        }
        super.close();
    }

    private void initiateTable( SQLiteDatabase db, RecordInfo recordInfo ) {
        for ( int recordIndex = 0 ; recordIndex < recordInfo.getmRecordList()
                .size() ; recordIndex++ ) {
            final ArrayList<Hashtable<String, String>> mRecords = recordInfo
                    .getmRecordList();

            Hashtable<String, String> record = new Hashtable<String, String>();

            record = mRecords.get( recordIndex );

            final ContentValues initialValues = new ContentValues();
            final Enumeration<String> keys = record.keys();
            String key, value;
            while ( keys.hasMoreElements() ) {

                key = keys.nextElement();
                value = record.get( key );
                initialValues.put( key, value );
            }
            db.insert( recordInfo.getmTableName(), null, initialValues );
        }
    }

    private void initiateTableStructure( SQLiteDatabase db, TableInfo table ) {
        String columnsCommand = GenericConverter
                .getCreateColumnsCommands( table );
        if ( columnsCommand == "" ) {
            this.close();
        }
        String CREATE_TABLES = "CREATE TABLE " + table.getTableName() + "("
                + columnsCommand + ");";
        db.execSQL( CREATE_TABLES );
        String message = table.getTableName() + " was created";
        this.mLogUtil.info( message, "initiateStructure" );
    }

    @Override
    public void onCreate( SQLiteDatabase db ) {
        this.recordInfoList = RecordInfoParser.parseRecordsTables( mContext,
                mTableDataFileId );
        this.tableInfoList = TableInfoParser.parserTableStructure( mContext,
                mTableStructureFileId );
        db.beginTransaction();
        try {
            for ( int i = 0 ; i < this.tableInfoList.size() ; i++ ) {
                TableInfo currentTable = this.tableInfoList.get( i );
                initiateTableStructure( db, currentTable );
            }
            db.setTransactionSuccessful();
        } catch ( android.database.SQLException e ) {
            throw e;
        } finally {
            db.endTransaction();
        }

        db.beginTransaction();
        try {
            for ( int i = 0 ; i < this.recordInfoList.size() ; i++ ) {
                RecordInfo recordInfo = this.recordInfoList.get( i );
                initiateTable( db, recordInfo );
            }
            db.setTransactionSuccessful();
        } catch ( android.database.SQLException e ) {
            throw e;
        } finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
        // TODO Auto-generated method stub
        super.onDowngrade( db, oldVersion, newVersion );
    }

    @Override
    public void onDowngrade( SQLiteDatabase db, int oldVersion, int newVersion ) {
        // TODO Auto-generated method stub
        super.onDowngrade( db, oldVersion, newVersion );
    }
}
